import 'package:flutter_module/beans/data_layer_item_bean.dart';
import 'package:flutter_module/map/ni_types.dart';
import 'package:flutter_module/util/string_utils.dart';

import 'base_bean.dart';

class DataLayer extends BaseBean {
  ///唯一标识
  String uuid;

  ///项目ID
  String projectId;

  ///图层名称
  String layerName;

  ///图层是否显示
  bool visibility;

  ///图层创建时间
  String createTime;

  ///图层数据类型，
  GeometryType geometryType;

  ///图层简介
  String describe;

  ///图层样式
  String style;

  List<DataLayerItem> itemList;

  DataLayer({
    this.uuid = "",
    this.projectId = "",
    this.geometryType = GeometryType.geometryTypePoint,
    this.layerName = "新建图层",
    required this.itemList,
    this.describe = "",
    this.style = "",
    this.visibility = false,
    this.createTime = "",
  }) {
    if (uuid == "") {
      uuid = StringUtil.createUUID();
    }
    if (createTime == "") {
      createTime = StringUtil.formatCurrentTime();
    }
  }

  @override
  Map<String, dynamic> toMap() {

    return {
      "uuid": uuid,
      "projectId": projectId,
      "geometryType": geometryType.index,
      "layerName": layerName,
      "import_time": createTime,
      "describe": describe,
      "visibility": visibility,
      "style": style,
      "layerItems": _itemsToMap()
    };
  }

  List<Map<String, dynamic>> _itemsToMap() {
    List<Map<String, dynamic>> list = [];
    for (int i = 0; i < itemList.length; i++) {
      list.add(itemList[i].toMap());
    }
    return list;
  }

  factory DataLayer.from(Map map) {
    var mapList = map["layerItems"];
    List<DataLayerItem> itemList = [];
    if (mapList != null && mapList is List) {
      for (var mapItem in mapList) {
        itemList.add(DataLayerItem.from(mapItem));
      }
    }
    return DataLayer(
        uuid: map["uuid"],
        projectId: map["projectId"],
        geometryType: GeometryType.values.elementAt(map["geometryType"]),
        layerName: map["layerName"],
        describe: map["describe"],
// style: map["style"],
        visibility: map["visibility"],
        createTime: map["import_time"],
        itemList: itemList);
  }
}
